{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01176b42",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fcf448d",
   "metadata": {},
   "source": [
    "## Pandas速查表2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e198fe6",
   "metadata": {},
   "source": [
    "### 数据信息获取，综合数据信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c43fda18",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('city_weather.csv')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d7a3bd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['city'].value_counts() #数出某一列独特的值的数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4fb13694",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(df) #长度，DataFrame数据有多少行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf46e49b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['city'].nunique() #数出某一列有多少个独特的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8717fb24",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.describe() #列出数据的明细表，推荐查看使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67f0f2be",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['wind'].sum() #某一列的值的和\n",
    "df['wind'].min() #某一列的值的最小值\n",
    "df['wind'].max() #某一列的值的最大值\n",
    "df['wind'].count() #某一列的值的数量\n",
    "df['wind'].median() #某一列的值的中位数\n",
    "df['wind'].mean() #某一列的值的平均值\n",
    "df['wind'].var() #某一列的值的方差\n",
    "df['wind'].std() #某一列的值的标准差\n",
    "df['wind'].quantile([0.25,0.75]) #某一列的值的四分位数\n",
    "df[['wind','temperature']].apply(np.sum) #整个数据值进行函数运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83f740db",
   "metadata": {},
   "source": [
    "### 缺失值判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "776a87cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.dropna() #把数据中为空的值删除，会删除行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66d27ca2",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.fillna(0) #把所有缺失值（空值）替换为fillna()中的内容"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95261be0",
   "metadata": {},
   "source": [
    "### 创建新的列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73a52fc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['Volume'] = df.wind\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c22cdffa",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.qcut(df.wind,3) #将数据切分分配到q中，x ：一维数组或者Seris q ： 表示分位数的整数或者数组，"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e62030d4",
   "metadata": {},
   "source": [
    "### 数据分组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3148bc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "g = df.groupby('city')#通过某一列进行分组\n",
    "#df.groupby(level='索引名') #进行索引分组\n",
    "#df.groupby(方法) #进行函数分组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5285ac9",
   "metadata": {},
   "outputs": [],
   "source": [
    "g.size() #查看每个分组的大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9432877",
   "metadata": {},
   "outputs": [],
   "source": [
    "#g.shift(1) #把每个组往下移动一行\n",
    "#g.shift(-1) #把每个组往上移动一行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74e31325",
   "metadata": {},
   "outputs": [],
   "source": [
    "#g.rank() #进行排名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8421caf8",
   "metadata": {},
   "outputs": [],
   "source": [
    "g.cumsum()\n",
    "#可以使用综合信息部分的函数\n",
    "g.sum()\n",
    "g.mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3a7eea9",
   "metadata": {},
   "source": [
    "### DataFrame组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28ace0f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1 = pd.DataFrame({\n",
    "    'x1':['A','B','C'],\n",
    "    'x2':[1,2,3]\n",
    "})\n",
    "df2 = pd.DataFrame({\n",
    "    'x1':['A','B','D'],\n",
    "    'x3':['T','F','T']\n",
    "})\n",
    "df1\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0d7b32d",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.merge(df1,df2,how='left',on='x1')\n",
    "pd.merge(df1,df2,how='right',on='x1')\n",
    "pd.merge(df1,df2,how='inner',on='x1')\n",
    "pd.merge(df1,df2,how='outer',on='x1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5a32f3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1[df1.x1.isin(df2.x1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5cdd6e60",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1[~df1.x1.isin(df2.x1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8116b866",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
